package com.cpu;

import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.function.BiConsumer;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author xuan.yang
 * @ClassName CpuBinGenerator
 * @description: TODO
 * @date 2024年03月23日 00:47:35
 * @version: 1.0
 */
public class CpuInstructionsBinGenerator {

    public static final int MSR	    =         	    1;
    public static final int MAR	    =         	    2;
    public static final int MDR	    =         	    3;
    /**
     * 内存
     */
    public static final int MC 	    =          	4;
    public static final int IR 	    =          	5;
    public static final int DST 	=         	6;
    public static final int SRC 	=         	7;
    public static final int A 	    =           8;
    public static final int B 	    =           9;
    public static final int C 	    =           10;
    public static final int D 	    =           11;
    public static final int DI 	    =          	12;
    public static final int SI 	    =          	13;
    public static final int SP 	    =          	14;
    public static final int BP 	    =          	15;
    public static final int CS 	    =          	16;
    public static final int DS 	    =          	17;
    public static final int SS 	    =          	18;
    public static final int ES 	    =          	19;
    public static final int VEC 	=         	20;
    public static final int T1 	    =          	21;
    public static final int T2 	    =          	22;

    /**
     * -----------------------------------out-------------------------------------
     */
    public static final int MSR_OUT	=         	MSR;
    public static final int MAR_OUT	=         	MAR;
    public static final int MDR_OUT	=         	MDR;
    /**
     * 内存
     */
    public static final int MC_OUT 	=          	MC;
    public static final int IR_OUT 	=          	IR;
    public static final int DST_OUT =         	DST;
    public static final int SRC_OUT =         	SRC;
    public static final int A_OUT 	=           A;
    public static final int B_OUT 	=           B;
    public static final int C_OUT 	=           C;
    public static final int D_OUT 	=           D;
    public static final int DI_OUT 	=          	DI;
    public static final int SI_OUT 	=          	SI;
    public static final int SP_OUT 	=          	SP;
    public static final int BP_OUT 	=          	BP;
    public static final int CS_OUT 	=          	CS;
    public static final int DS_OUT 	=          	DS;
    public static final int SS_OUT 	=          	SS;
    public static final int ES_OUT 	=          	ES;
    public static final int VEC_OUT =         	VEC;
    public static final int T1_OUT 	=          	T1;
    public static final int T2_OUT 	=          	T2;


    public static final int OFFSET =            5;
    /**
     * -----------------------------------in-------------------------------------
     */
    public static final int MSR_IN	=         	MSR << OFFSET;
    public static final int MAR_IN	=         	MAR << OFFSET;
    public static final int MDR_IN	=         	MDR << OFFSET;
    /**
     * 内存
     */
    public static final int MC_IN 	=          	MC << OFFSET;
    public static final int IR_IN 	=          	IR << OFFSET;
    public static final int DST_IN 	=         	DST << OFFSET;
    public static final int SRC_IN 	=         	SRC << OFFSET;
    public static final int A_IN    =           A << OFFSET;
    public static final int B_IN    =           B << OFFSET;
    public static final int C_IN    =           C << OFFSET;
    public static final int D_IN    =           D << OFFSET;
    public static final int DI_IN 	=          	DI << OFFSET;
    public static final int SI_IN 	=          	SI << OFFSET;
    public static final int SP_IN 	=          	SP << OFFSET;
    public static final int BP_IN 	=          	BP << OFFSET;
    public static final int CS_IN 	=          	CS << OFFSET;
    public static final int DS_IN 	=          	DS << OFFSET;
    public static final int SS_IN 	=          	SS << OFFSET;
    public static final int ES_IN 	=          	ES << OFFSET;
    public static final int VEC_IN 	=         	VEC << OFFSET;
    public static final int T1_IN 	=          	T1 << OFFSET;
    public static final int T2_IN 	=          	T2 << OFFSET;


//    public static List<int> generate() {
//        // 1、从第一个内存位置读取数据 并 放入A寄存器
//        int step1 = MEM_CS | A_CS | A_WE | PC_CS | PC_WE | PC_EN;
//        // 2、从第二个内存位置读取数据 并 放入B寄存器
//        int step2 = MEM_CS | B_CS | B_WE | PC_CS | PC_WE | PC_EN;
//        // 3、（此处需要提前使A、B寄存器同使输出工作）ALU做计算 并 输出 到 C寄存器
//        int step3 = ALU_EN | ALU_OP_ADD | C_CS | C_WE;
//        // 4、（此处前提需要CS工作）将C寄存器结果输出 并 将结果输出到 内存
//        int step4 = C_CS | MEM_CS | MEM_WE | PC_WE | PC_CS | PC_EN;
//        // 5、停止
//        int step5 = STOP;
//        return Stream.of(step1, step2, step3, step4, step5).collect(Collectors.toList());
//    }
//
//    public static void main(String[] args) throws IOException {
//        BiConsumer<DataOutputStream, int> fileWriteIntF = (dos, i) -> {
//            try {
//                System.out.println(String.format("%016X", i).substring(12));
//                dos.writeint(i);
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//        };
//        try (DataOutputStream bw = new DataOutputStream(new FileOutputStream("./add.bin"))) {
//            generate().forEach(x -> fileWriteIntF.accept(bw, x));
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }


}
